﻿/**
* @description  Class that stores mapping of classes and their skin properties to skin classes that draw/render.
*/
package com.foed.managers {

	import com.foed.IDescribable;

	public class SkinManager {

		private static var sManager:SkinManager;
		private var _skins:Object;

		/**
		* @description  Constructor for Singleton class.
		*/
		public function SkinManager() {
			_skins = {};
		}

		/**
		* @description  Returns only instance of Singleton class.
		*
		* returns  The single SkinManager instance.
		*/
		static public function getInstance():SkinManager {
			if (sManager == null) sManager = new SkinManager();
			return sManager;
		}

		/**
		* @description  Sets the mapping of a class and its skin properties to skin classes.
		*
		* @param  pClass  The fully qualified name of the class to map skins classes to.
		* @param  pSkins  An object that maps the names of skin properties to skin class references.
		*/
		public function setClassSkins(pClass:String, pSkins:Object):void {
			_skins[pClass] = pSkins;
		}

		/**
		* @description  Returns the proper skin class, if any, for the object and property passed in.
		*
		* @param  pObj  The instance with a skin property to find a class for.
		* @param  pProp  The skin property that needs a class to be drawn with.
		*
		* @returns  The skin class to draw for the specified object's skin property.
		*/
		public function getSkin(pObj:IDescribable, pProp:String):Class {
			var pClasses:Array = pObj.getClassHierarchy();
			var pClass:String;
			var i:int = pClasses.length;
			while (--i > -1) {
				pClass = pClasses[i];
				if (_skins[pClass] != null) {
					if (_skins[pClass][pProp] != null) {
						return _skins[pClass][pProp];
					}
				}
			}
			return null;
		}

	}

}